package me.jessyan.mvparms.demo.mvp.ui.activity.coupon;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DoubleClickUtils;
import com.jess.arms.utils.LogUtils;
import com.jess.arms.utils.StautBarUtils;
import com.jess.arms.widget.CustomPopupWindow;

import javax.inject.Inject;
import javax.inject.Named;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.di.component.coupon.DaggerCouponDetailComponent;
import me.jessyan.mvparms.demo.di.module.coupon.CouponDetailModule;
import me.jessyan.mvparms.demo.mvp.contract.coupon.CouponDetailContract;
import me.jessyan.mvparms.demo.mvp.presenter.coupon.CouponDetailPresenter;
import me.jessyan.mvparms.demo.mvp.ui.widget.CustomToolbar;
import me.jessyan.mvparms.demo.mvp.ui.widget.ShadowLinearLayout;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 停车券详情
 */
public class CouponDetailActivity extends BaseActivity<CouponDetailPresenter> implements CouponDetailContract.View, PopupWindow.OnDismissListener {

    // 专用优惠券
    public static final int COUPON_SPECIAL = 1;
    //通用优惠券
    public static final int COUPON_GENERAL = 2;
    //停车券
    public static final int COUPON_PARKING = 3;
    //抢券
    public static final int COUPON_ROB = 4;
    @BindView(R.id.toolbar)
    CustomToolbar toolbar;
    @BindView(R.id.use_tv)
    TextView useTv;
    @BindView(R.id.shadow_ll)
    ShadowLinearLayout shadowLl;
    @BindView(R.id.shop_head_iv)
    ImageView shopHeadIv;
    @BindView(R.id.shop_detail)
    TextView shopDetail;
    @BindView(R.id.shop_detail_pic_iv)
    ImageView shopDetailPicIv;
    @BindView(R.id.use_need_tv)
    TextView useNeedTv;
    @BindView(R.id.apply_shore_tv)
    TextView applyShoreTv;
    @BindView(R.id.apply_shore_name_tv)
    TextView applyShoreNameTv;
    @BindView(R.id.cl)
    ConstraintLayout cl;
    @BindView(R.id.fl)
    FrameLayout fl;


    private int type = 1;

    /**
     * 是否过期
     */
    private boolean isPastDue = false;

    @Inject
    @Named("deletePop")
    CustomPopupWindow deletePop;
    @Inject
    @Named("sharePop")
    CustomPopupWindow sharePop;
    @Inject
    @Named("sureDeletePop")
    CustomPopupWindow sureDeletePop;
    @Inject
    @Named("giftCardPop")
    CustomPopupWindow giftCardPop;
    @Inject
    @Named("QRCodePop")
    CustomPopupWindow QRCodePop;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        Intent intent = getIntent();
        type = intent.getIntExtra("type",1);
        isPastDue = intent.getBooleanExtra("isPastDue",false);
        LogUtils.debugInfo("type"+type+" isPastDue:"+isPastDue);

                DaggerCouponDetailComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .couponDetailModule(new CouponDetailModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_coupon_detail; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        StautBarUtils.setStatusMode(this, false, true);
        StautBarUtils.setBarPadding(cl);
        StautBarUtils.setBarPadding(findViewById(R.id.fl));
        if (type != COUPON_ROB) {//抢券是没有删除和分享的
            toolbar.setRightIv(R.drawable.ic_toolbar_point_withe);

        }
        if(type==COUPON_PARKING){
            toolbar.setTitle("停车优惠券");
        }

        toolbar.setListener(new CustomToolbar.ToolbarListener() {
            @Override
            public void onLeftBack() {
                killMyself();
            }

            @Override
            public void onRightBack() {
                showDeletePop();
            }
        });

        sureDeletePop.setOnDismissListener(this);
        sharePop.setOnDismissListener(this);
        giftCardPop.setOnDismissListener(this);
        deletePop.setOnDismissListener(this);
        QRCodePop.setOnDismissListener(this);

        initV();


    }

    private void initV() {
        switch (type) {
            //专用
            case COUPON_SPECIAL:
                setUISpecial();
                break;
            //通用
            case COUPON_GENERAL:
                setUIGeneral();
                break;
            //停车券
            case COUPON_PARKING:
                setUiParking();
                break;
            //抢券
            case COUPON_ROB:
                setUIROb();
                break;
            default:
        }
        if (isPastDue) {
            useTv.setText("已使用（意思意思）");
            useTv.setEnabled(false);
        }
    }

    /**
     * 停车券
     */
    private void setUiParking() {
        setUIGeneral();
        cl.setBackgroundResource(R.drawable.gr_3d16d5_527df6);
    }

    /**
     * 抢券
     */
    private void setUIROb() {
        setUISpecial();
        useTv.setText("立即领取");
    }

    /**
     * 设置通用券
     */
    private void setUIGeneral() {
        shopDetail.setVisibility(View.GONE);
        shopDetailPicIv.setVisibility(View.GONE);
        useNeedTv.setVisibility(View.VISIBLE);
        applyShoreTv.setVisibility(View.VISIBLE);
        applyShoreNameTv.setVisibility(View.VISIBLE);
    }

    /**
     * 设置专用券
     */
    private void setUISpecial() {
        shopDetail.setVisibility(View.VISIBLE);
        shopDetailPicIv.setVisibility(View.VISIBLE);
        useNeedTv.setVisibility(View.VISIBLE);
        applyShoreTv.setVisibility(View.GONE);
        applyShoreNameTv.setVisibility(View.GONE);

    }


    /**
     * 显示删除那个pop
     */
    private void showDeletePop() {
        if (isPastDue) {
            if (deletePop != null) {
                //这个东西再7.0 以后会有问题
                if (Build.VERSION.SDK_INT >= 24) {

                    deletePop.measureHeight();
                    deletePop.showAsDropDown(toolbar.getRightIv(), -ArmsUtils.dip2px(getApplication(), 74), 0, Gravity.LEFT);
                } else {

                    deletePop.showAsDropDown(toolbar.getRightIv(), -ArmsUtils.dip2px(getApplication(), 74), 0, Gravity.LEFT);
                }

            }
        } else {
            if (sharePop != null) {
                //这个东西再7.0 以后会有问题
                if (Build.VERSION.SDK_INT >= 24) {

                    sharePop.measureHeight();
                    sharePop.showAsDropDown(toolbar.getRightIv(), -ArmsUtils.dip2px(getApplication(), 74), 0, Gravity.LEFT);
                } else {

                    sharePop.showAsDropDown(toolbar.getRightIv(), -ArmsUtils.dip2px(getApplication(), 74), 0, Gravity.LEFT);
                }

            }
        }


    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @Override
    public void onDismiss() {
        backgroundAlpha(1);
    }

    @Override
    public CouponDetailActivity getActivity() {
        return this;
    }

    @Override
    public void dismissPop() {

        if (sureDeletePop != null && sureDeletePop.isShowing()) {
            sureDeletePop.dismiss();
        }
        if (sharePop != null && sharePop.isShowing()) {
            sharePop.dismiss();
        }
        if (giftCardPop != null && giftCardPop.isShowing()) {
            giftCardPop.dismiss();
        }
        if (deletePop != null && deletePop.isShowing()) {
            deletePop.dismiss();
        }
        if(QRCodePop!=null &&QRCodePop.isShowing()){
            QRCodePop.dismiss();
        }


    }

    @Override
    public void popBackDelete() {
        if (sureDeletePop != null) {
            sureDeletePop.show();
            backgroundAlpha(0.7f);
        }
    }

    @Override
    public void popBackShare() {
        if (giftCardPop != null) {
            giftCardPop.show();
            backgroundAlpha(0.7f);
        }
    }

    @Override
    public void popBackSureDelete() {
        finish();
    }

    @Override
    public void popBackGiftCard(String phone) {
        showMessage(phone);
    }



    @OnClick({R.id.use_tv, R.id.use_need_tv, R.id.apply_shore_tv})
    public void onViewClicked(View view) {
        if(DoubleClickUtils.getInstance().isDouble(view)){
            return;
        }
        switch (view.getId()) {
            case R.id.use_tv:
                if(QRCodePop!=null){
                    QRCodePop.show();
                    backgroundAlpha(0.7f);
                }
                break;
            case R.id.use_need_tv:
                break;
            case R.id.apply_shore_tv:
                break;
        }
    }
}
